
package com.aries.ui.widget;

import com.aries.ui.helper.navigation.KeyboardHelper;
import com.aries.ui.helper.navigation.NavigationViewHelper;
import com.aries.ui.util.AnimationUtils;
import com.aries.ui.util.ColorStateListBean;
import com.aries.ui.util.FindViewUtil;
import com.aries.ui.util.ResourceUtil;
import com.aries.ui.view.DragLayout;
import com.aries.ui.view.title.ColorStateList;
import com.aries.ui.widget.i.DialogInterface;
import com.aries.ui.widget.i.NavigationBarControl;
import ohos.aafwk.ability.Ability;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.render.render3d.BuildConfig;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextTool;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * @Author: AriesHoo on 2018/7/19 8:46
 * @E-Mail: AriesHoo@126.com
 * Function: Widget Dialog模式基类
 * Description:
 * 1、新增基础Builder包装通用属性
 * 2、新增ContentView margin属性
 * 3、新增控制虚拟导航栏功能
 * 4、2018-4-3 12:51:47 移除控制虚拟导航栏功能
 * 5、2018-7-19 09:02:00 修改点击contentView 父容器逻辑处理
 * 6、2019-4-11 12:40:03 新增底部虚拟导航栏控制{@link #setNavigationBarControl(NavigationBarControl)}
 * 修改{ #afterSetContentView(View)}逻辑
 * 7、2019-4-18 11:39:46 修改{@link #backDialog(boolean)}逻辑使其可即时生效
 * 8、2019-4-22 16:50:33 新增{@link #setDragEnable(boolean)}手指跟随滑动关闭功能
 */
public class BasisDialog<T extends BasisDialog> extends CommonDialog implements BaseDialog.DisplayCallback {

    public static final int COMPONENT_STATE_LONG_PRESSED = 18432;
    public static final String UI_ACTION = "ui_action";
    public boolean isUseAnimation = true;
    protected Window mWindow;
    protected Component mContentView;
    protected boolean mCanceledOnTouchOutside;
    protected DialogInterface.OnDismissListener mOnDismissListener;
    protected DialogInterface.OnKeyListener mOnKeyListener;
    protected DialogInterface.OnCancelListener mOnCancelListener;
    protected DialogInterface.OnShowListener mOnShowListener;
    /**
     * 是否允许手指拖动关闭
     */
    protected boolean mDragEnable = false;
    protected NavigationBarControl mNavigationBarControl;
    /**
     * 虚拟导航栏底部View
     */
    protected Component mNavigationBottomView;
    private WindowManager.LayoutConfig mLayoutParams;
    //    private DisplayAttributes mLayoutParams;
    private float mAlpha = 1.0f;
    private float mDimAmount = 0.6f;
    private int mGravity = LayoutAlignment.CENTER;
    private int mWidth = DirectionalLayout.LayoutConfig.MATCH_CONTENT;
    private int mHeight = DirectionalLayout.LayoutConfig.MATCH_CONTENT;
    private int mWindowAnimations = -1;
    private NavigationViewHelper mNavigationViewHelper;
    private String type;
    private Ability mAbility;
    private Context mContext;
    /**
     * 是否触摸到外面
     */
    private DialogListener dialogListener = new DialogListener() {
        @Override
        public boolean isTouchOutside() {
            if (mCanceledOnTouchOutside) {
                hide();
            }
            return mCanceledOnTouchOutside;
        }
    };

    public BasisDialog(Context context) {
        super(context);
        mContext = context;
        if (context instanceof Ability) {
//            setOwnerActivity((Ability) context);
//            setOwnerActivity((Ability) context);
            mAbility = ((Ability) context);
        }

    }

    public Ability getContext() {
        return mAbility;
    }

    public void setType(String dialogType) {
        this.type = dialogType;
    }

    public interface OnTextViewLineListener {
        /**
         * TextView及其控件行数变化监听
         *
         * @param textView
         * @param lineCount
         */
        void onTextViewLineListener(Text textView, int lineCount);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mWindow = getWindow();
        if (mLayoutParams == null) {
            mLayoutParams = mWindow.getLayoutConfig().get();
        }
        mLayoutParams.width = mWidth;
        mLayoutParams.height = mHeight;
        mLayoutParams.alignment = mGravity;
        // 透明度
        mLayoutParams.alpha = mAlpha;
        // 黑暗度
//        mLayoutParams.dimAmount = mDimAmount;
        mLayoutParams.dim = mDimAmount;
        if (mWindowAnimations != -1) {
            // 动画
//            mLayoutParams.windowAnimations = mWindowAnimations;
            mLayoutParams.animations = mWindowAnimations;
        }

        mWindow.setLayoutConfig(mLayoutParams);
        mWindow.setLayoutAlignment(LayoutAlignment.CENTER);
        if (mDragEnable) {
            setDragEnable(true);
        }

        registerDisplayCallback(this);
        mNavigationBottomView = mContentView;

    }


    @Override
    public CommonDialog setContentCustomComponent(Component component) {
        this.mContentView = component;
//        if (mContentView != null) {
//            afterSetContentView(mContentView);
//        }
//        StatusBarUtil.fitsNotchScreen(getWindow(), true);
//        Activity activity = getOwnerActivity();
//        Ability activity = mAbility;
//        if (activity == null || mNavigationBarControl == null) {
//            return null;
//        }
//        mNavigationViewHelper = NavigationViewHelper.with(activity, this)
//                .setLogEnable(BuildConfig.DEBUG)
//                .setControlEnable(true)
//                .setTransEnable(true)
//                .setBottomView(mNavigationBottomView);
//        boolean isInit = mNavigationBarControl.setNavigationBar(this, mNavigationViewHelper, mNavigationBottomView);
//        if (isInit) {
//            mNavigationViewHelper.init();
//        }
        return super.setContentCustomComponent(component);
    }

    public void setContentView(Component view, ComponentContainer.LayoutConfig params) {
        view.setLayoutConfig(params);
        super.setContentCustomComponent(view);
        this.mContentView = view;
    }

    public void setCanceledOnTouchOutside(boolean cancel) {
        this.mCanceledOnTouchOutside = cancel;
        setDialogListener(dialogListener);
    }

    @Override
    protected void onWindowSelectionUpdated(boolean isSelectionUpdated) {
        super.onWindowSelectionUpdated(isSelectionUpdated);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mNavigationViewHelper != null) {
            mNavigationViewHelper.onDestroy();
        }
        mNavigationViewHelper = null;
    }

    protected void afterSetContentView(Component parent) {
        //当设置点击其它地方dialog可消失需对root的父容器处理
        if (mCanceledOnTouchOutside && parent != null) {
            //设置点击事件防止事件透传至父容器
            parent.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (!TextTool.isNullOrEmpty(type) && type.equals(UI_ACTION)) {
                        if (isUseAnimation) {
                            AnimationUtils.getInstance(getContext()).hideDialogAnimation(BasisDialog.this);
                        }else{
                            hide();
                        }
                    } else {
                        hide();
                    }
                }
            });

            ComponentContainer componentParent = (ComponentContainer) parent.getComponentParent();
            if (componentParent != null) {
                componentParent.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        if (!TextTool.isNullOrEmpty(type) && type.equals(UI_ACTION)) {
                            if (isUseAnimation) {
                                AnimationUtils.getInstance(getContext()).hideDialogAnimation(BasisDialog.this);
                            } else {
                                hide();
                            }
                        } else {
                            hide();
                        }
                    }
                });
            }

        }
    }

    /**
     * 获取根布局
     *
     * @return 获取根布局
     */
    public Component getContentView() {
        return mContentView;
    }

    public void setContentView(int layoutResID) {
        Component view = LayoutScatter.getInstance(mContext).parse(layoutResID, null, false);
        setContentView(view);
    }

    public void setContentView(Component view) {
        ComponentContainer.LayoutConfig params = view != null ? view.getLayoutConfig() : null;
        if (params == null) {
            params = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
        }
        view.setLayoutConfig(params);
        setContentCustomComponent(view);
    }

    protected T backDialog() {
        return backDialog(false);
    }

    protected T backDialog(boolean attributes) {
        if (attributes && mWindow != null && mLayoutParams != null) {
            mLayoutParams.width = mWidth;
            mLayoutParams.height = mHeight;
            mLayoutParams.alignment = mGravity;
            // 透明度
            mLayoutParams.alpha = mAlpha;
            // 黑暗度
            mLayoutParams.dim = mDimAmount;
            if (mWindowAnimations != -1) {
                // 动画
                mLayoutParams.animations = mWindowAnimations;
            }
            mWindow.setLayoutConfig(mLayoutParams);
            mWindow.setLayoutAlignment(mGravity);
        }
        return (T) this;
    }

    public T setAttributes(WindowManager.LayoutConfig params) {
        this.mLayoutParams = params;
        return backDialog(true);
    }

    public T setAlpha(float alpha) {
        mAlpha = alpha;
        return backDialog(true);
    }

    public T setDimAmount(float dimAmount) {
        mDimAmount = dimAmount;
        return backDialog(true);
    }

    public T setGravity(int gravity) {
        mGravity = gravity;
        return backDialog(true);
    }

    public T setWidth(int w) {
        this.mWidth = w;
        return backDialog(true);
    }

    public T setHeight(int h) {
        this.mHeight = h;
        return backDialog(true);
    }

    /**
     * 设置动画资源
     *
     * @param res res
     * @return 设置动画资源
     */
    public T setWindowAnimations(int res) {
        this.mWindowAnimations = res;
        return backDialog(true);
    }

    /**
     * 是否开启手指拖拽
     *
     * @param enable enable
     * @return 是否开启手指拖拽
     */
    public T setDragEnable(boolean enable) {
        this.mDragEnable = enable;
        if (mContentView != null) {
            DragLayout dragLayout = FindViewUtil.getTargetView(mContentView, DragLayout.class);
            if (dragLayout != null) {
                dragLayout.setDragEnable(mDragEnable);
            } else {
                ComponentContainer group = (ComponentContainer) mContentView.getComponentParent();
                if (group != null) {
                    group.removeComponent(mContentView);
                    dragLayout = new DragLayout(mContext);
                    dragLayout.addComponent(mContentView);
                    dragLayout.setDragEnable(mDragEnable);
                    dragLayout.setOnDragListener(new DragLayout.OnDragListener() {
                        @Override
                        public void onClosed() {
                            hide();
                        }

                        @Override
                        public void onOpened() {

                        }
                    });
                    mContentView = dragLayout;
                    group.addComponent(dragLayout);
                }
            }
        }
        return backDialog();
    }

    /**
     * 设置ContentView margin属性
     *
     * @param left   left
     * @param top    top
     * @param right  right
     * @param bottom bottom
     * @return 设置ContentView margin属性
     */
    public T setMargin(int left, int top, int right, int bottom) {
        ComponentContainer.LayoutConfig params = (ComponentContainer.LayoutConfig) mContentView.getLayoutConfig();
        if (params != null) {
            params.setMargins(left, top, right, bottom);
            mContentView.setLayoutConfig(params);
        }
        return backDialog();
    }

    /**
     * 设置导航栏控制
     *
     * @param control control
     * @return 设置导航栏控制
     */
    public T setNavigationBarControl(NavigationBarControl control) {
        this.mNavigationBarControl = control;
        return backDialog();
    }

    protected void closeKeyboard() {
        KeyboardHelper.closeKeyboard(this);
    }

    @Override
    protected void onHide() {
//        closeKeyboard();
        super.onHide();
        if (mOnDismissListener != null) {
            mOnDismissListener.onDismiss(this);
        }
    }

    @Override
    protected void onShowing() {
        super.onShowing();
    }

    @Override
    public void onDisplay(IDialog iDialog) {
        if (mOnShowListener != null) {
            mOnShowListener.onShow(this);
        }

        if (mContentView != null) {
            afterSetContentView(mContentView);
        }

//                StatusBarUtil.fitsNotchScreen(getWindow(), true);
//        Activity activity = getOwnerActivity();
        Ability activity = mAbility;
        if (activity == null || mNavigationBarControl == null) {
            return;
        }
        mNavigationViewHelper = NavigationViewHelper.with(activity, this)
                .setLogEnable(BuildConfig.DEBUG)
                .setControlEnable(true)
                .setTransEnable(true)
                .setBottomView(mNavigationBottomView);
        boolean isInit = mNavigationBarControl.setNavigationBar(this, mNavigationViewHelper, mNavigationBottomView);
        if (isInit) {
            mNavigationViewHelper.init();
        }


    }

    private void setOnDismissListener(DialogInterface.OnDismissListener mOnDismissListener) {
        this.mOnDismissListener = mOnDismissListener;
    }

    private void setOnShowListener(DialogInterface.OnShowListener onShowListener) {
        mOnShowListener = onShowListener;
    }


    /**
     * @param <T>
     */
    protected static class BasisBuilder<T extends BasisBuilder> {

        protected int mStatePressed = ComponentState.COMPONENT_STATE_PRESSED;
        protected int mStateEmpty = ComponentState.COMPONENT_STATE_EMPTY;
        protected Context mContext;
        protected ResourceUtil mResourceUtil;
        protected BasisDialog mDialog;
        protected DirectionalLayout mLLayoutRoot;

        protected Element mBackground;
        protected float mBackgroundRadius;
        protected float mElevation;
        protected int mPadding;
        protected int mMinWidth;
        protected int mMinHeight;

        protected float mLineSpacingMultiplier = 1.0f;
        protected float mLineSpacingExtra = 0.0f;
        protected Text.TextSizeType mTextSizeUnit = Text.TextSizeType.VP;

        protected boolean mCancelable = true;
        protected boolean mCanceledOnTouchOutside = true;
        protected boolean mDragEnable = false;
        protected OnTextViewLineListener mOnTextViewLineListener;
        protected DialogInterface.OnDismissListener mOnDismissListener;
        protected DialogInterface.OnKeyListener mOnKeyListener;
        protected DialogInterface.OnCancelListener mOnCancelListener;
        protected DialogInterface.OnShowListener mOnShowListener;
        protected NavigationBarControl mNavigationBarControl;
        protected Display display;


        public BasisBuilder(Context context) {
            mContext = context;
            display = DisplayManager.getInstance().getDefaultDisplay(mContext).get();
            mResourceUtil = new ResourceUtil(mContext);
        }

        protected T backBuilder() {
            return (T) this;
        }

        /**
         * 设置根布局背景颜色值
         *
         * @param color color
         * @return 设置根布局背景颜色值
         */
        public T setBackgroundColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setBackground(shapeElement);
//            return setBackground(new ColorDrawable(color));
        }

        /**
         * 设置根布局背景资源
         *
         * @param resId resId
         * @return 设置根布局背景资源
         */
        public T setBackgroundResource(int resId) {
            return setBackground(mResourceUtil.getColorDrawable(resId));
        }

        /**
         * 设置背景圆角--背景为颜色值时有效
         *
         * @param radius radius
         * @return backBuilder
         */
        public T setBackgroundRadius(float radius) {
            mBackgroundRadius = radius;
            return backBuilder();
        }

        public T setBackgroundRadiusResource(int res) {
            return setBackgroundRadius(mResourceUtil.getDimension(res));
        }

        /**
         * 设置根布局海拔
         *
         * @param elevation elevation
         * @return backBuilder
         */
        public T setElevation(float elevation) {
            this.mElevation = elevation;
            return backBuilder();
        }

        public T setElevationResoure(int res) {
            return setElevation(mResourceUtil.getDimension(res));
        }

        /**
         * 设置根布局padding值
         *
         * @param padding padding
         * @return backBuilder
         */
        public T setPadding(int padding) {
            this.mPadding = padding;
            return backBuilder();
        }

        /**
         * 设置根布局最小宽度
         *
         * @param w w
         * @return backBuilder
         */
        public T setMinWidth(int w) {
            this.mMinWidth = w;
            return backBuilder();
        }

        public T setMinWidthResource(int res) {
            return setMinWidth(mResourceUtil.getDimensionPixelSize(res));
        }

        /**
         * 设置根布局最小高度
         *
         * @param h h
         * @return backBuilder
         */
        public T setMinHeight(int h) {
            this.mMinHeight = h;
            return backBuilder();
        }

        public T setMinHeightResource(int res) {
            return setMinHeight(mResourceUtil.getDimensionPixelSize(res));
        }

        /**
         * 设置所有文本行间距属性
         * { TextView#setLineSpacing(float, float)}
         *
         * @param lineSpacingMultiplier lineSpacingMultiplier
         * @return backBuilder
         */
        public T setLineSpacingMultiplier(float lineSpacingMultiplier) {
            mLineSpacingMultiplier = lineSpacingMultiplier;
            return backBuilder();
        }

        /**
         * { TextView#setLineSpacing(float, float)}
         *
         * @param lineSpacingExtra lineSpacingExtra
         * @return backBuilder
         */
        public T setLineSpacingExtra(float lineSpacingExtra) {
            mLineSpacingExtra = lineSpacingExtra;
            return backBuilder();
        }

        /**
         * 设置TextView的尺寸单位
         * { TextView#setTextSize(int, float)}
         *
         * @param unit unit
         * @return backBuilder
         */
        public T setTextSizeUnit(Text.TextSizeType unit) {
            this.mTextSizeUnit = unit;
            return backBuilder();
        }

        /**
         * 设置dialog 是否可点击返回键关闭
         * {@link #setCancelable(boolean)}
         *
         * @param enable enable
         * @return backBuilder
         */
        public T setCancelable(boolean enable) {
            this.mCancelable = enable;
            return backBuilder();
        }

        /**
         * 点击非contentView 是否关闭dialog
         * {@link #setCanceledOnTouchOutside(boolean)}
         *
         * @param enable enable
         * @return backBuilder
         */
        public T setCanceledOnTouchOutside(boolean enable) {
            this.mCanceledOnTouchOutside = enable;
            return backBuilder();
        }

        /**
         * 是否开启手指拖拽--通过添加
         *
         * @param enable enable
         * @return backBuilder
         */

        public T setDragEnable(boolean enable) {
            this.mDragEnable = enable;
            return backBuilder();
        }

        /**
         * 设置TextView 文本行数监听
         * { TextView#post(Runnable)}
         *
         * @param listener listener
         * @return backBuilder
         */
        public T setOnTextViewLineListener(OnTextViewLineListener listener) {
            this.mOnTextViewLineListener = listener;
            return backBuilder();
        }

        /**
         * 设置dialog消失监听
         * { #setOnDismissListener(OnDismissListener)}
         *
         * @param listener listener
         * @return backBuilder
         */
        public T setOnDismissListener(DialogInterface.OnDismissListener listener) {
            this.mOnDismissListener = listener;
            return backBuilder();
        }

        /**
         * 设置dialog 键盘事件监听
         * { #setOnKeyListener(OnKeyListener)}
         *
         * @param listener listener
         * @return backBuilder
         */
        public T setOnKeyListener(DialogInterface.OnKeyListener listener) {
            this.mOnKeyListener = listener;
            return backBuilder();
        }

        /**
         * 设置dialog显示事件监听
         * { #setOnShowListener(OnShowListener)}
         *
         * @param listener listener
         * @return backBuilder
         */
        public T setOnShowListener(DialogInterface.OnShowListener listener) {
            this.mOnShowListener = listener;
            return backBuilder();
        }

        /**
         * 设置Dialog Cancel监听
         * { #setOnCancelListener(OnCancelListener)}
         *
         * @param listener listener
         * @return backBuilder
         */
        public T setOnCancelListener(DialogInterface.OnCancelListener listener) {
            this.mOnCancelListener = listener;
            return backBuilder();
        }

        /**
         * 设置导航栏控制
         *
         * @param control control
         * @return backBuilder
         */
        public T setNavigationBarControl(NavigationBarControl control) {
            this.mNavigationBarControl = control;
            return backBuilder();
        }

        /**
         * 设置Dialog通用属性
         */
        protected void setDialog() {
            if (mDialog == null) {
                return;
            }
            mDialog.setDragEnable(mDragEnable);
//            mDialog.setCancelable(mCancelable);
            mDialog.siteRemovable(mCancelable);
            mDialog.setCanceledOnTouchOutside(mCanceledOnTouchOutside);

            if (mOnDismissListener != null) {
                mDialog.setOnDismissListener(mOnDismissListener);
            }
//            if (mOnKeyListener != null) {
//                mDialog.setOnKeyListener(mOnKeyListener);
//            }
//            if (mOnCancelListener != null) {
//                mDialog.setOnCancelListener(mOnCancelListener);
//            }
            if (mOnShowListener != null) {
                mDialog.setOnShowListener(mOnShowListener);
            }
            if (mNavigationBarControl != null) {
                mDialog.setNavigationBarControl(mNavigationBarControl);
            }
        }

        protected void setRootView() {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                mLLayoutRoot.setElevation(mElevation);
//            }
            mLLayoutRoot.removeAllComponents();
            mLLayoutRoot.setPadding(mPadding, mPadding, mPadding, mPadding);
            mBackground = getBackground();
            if (mBackground != null) {
                setViewBackground(mLLayoutRoot, mBackground);
            }
        }

        /**
         * 通用设置View 背景Drawable
         *
         * @param view     view
         * @param drawable drawable
         */
        protected void setViewBackground(Component view, Element drawable) {
            if (view == null) {
                return;
            }

//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//                view.setBackground(DrawableUtil.getNewDrawable(drawable));
//            } else {
//                view.setBackgroundDrawable(DrawableUtil.getNewDrawable(drawable));
//            }

//            if (drawable instanceof StateElement) {
//                StateElement element = (StateElement) drawable;
//                if (drawable != null && element.getStateCount() > 0) {
//                    if (!checkElementIsNull(element,1)){
//                        view.setBackground(element.getStateElement(1).getCurrentElement());
//                    };
//                    view.setComponentStateChangedListener(new Component.ComponentStateChangedListener() {
//                        @Override
//                        public void onComponentStateChanged(Component component, int i) {
//                            if (i == ComponentState.COMPONENT_STATE_PRESSED) {
//                                if (!checkElementIsNull(element,0)){
//                                    view.setBackground(element.getStateElement(0).getCurrentElement());
//                                }
//
//                            } else {
//                                if (!checkElementIsNull(element,1)){
//                                    view.setBackground(element.getStateElement(1).getCurrentElement());
//                                };
//                            }
//                        }
//                    });
//                }
//
//            } else {
            view.setBackground(drawable);
//            }


        }

        /**
         * 判断当前Element 并且取出来的值是否是空的 是否是空的
         *
         * @param element element
         * @param index   index
         * @return 是否是空的
         */
        private boolean checkElementIsNull(StateElement element, int index) {
            Element stateElement = element.getStateElement(index);
            if (stateElement != null) {
                return false;
            }

            return true;
        }

        /**
         * 设置TextView属性
         *
         * @param textView     textView
         * @param sequence     sequence
         * @param textColor    textColor
         * @param textSize     textSize
         * @param gravity      gravity
         * @param fakeBoldText fakeBoldText
         */
        protected void setTextAttribute(Text textView, String sequence,
                                        ColorStateListBean textColor, int textSize, int gravity, boolean fakeBoldText) {
            if (textView == null) {
                return;
            }
            textView.setLineSpacing(mLineSpacingExtra, mLineSpacingMultiplier);
            textView.setTextAlignment(gravity);
            textView.setText(sequence);
            textView.setTextSize(textSize, mTextSizeUnit);

            if (fakeBoldText) {
                textView.setFont(Font.DEFAULT_BOLD);
            }
//            textView.getPaint().setFakeBoldText(fakeBoldText);

            if (ComponentState.COMPONENT_STATE_EMPTY == textColor.getState()) {
                textView.setTextColor((new Color(textColor.getColor(ComponentState.COMPONENT_STATE_EMPTY))));

            }

            textView.setComponentStateChangedListener(new Component.ComponentStateChangedListener() {
                @Override
                public void onComponentStateChanged(Component component, int state) {
//                        if (ComponentState.isStateMatched(state, ComponentState.COMPONENT_STATE_PRESSED)){
                    if (state == BasisDialog.COMPONENT_STATE_LONG_PRESSED || ComponentState.isStateMatched(state, ComponentState.COMPONENT_STATE_PRESSED)) {
                        textView.setTextColor(textColor.getColor(state) == 0 ? textView.getTextColor() : new Color(textColor.getColor(state)));
                    } else {
                        textView.setTextColor(textColor.getColor(state) == 0 ? textView.getTextColor() : (new Color(textColor.getColor(ComponentState.COMPONENT_STATE_EMPTY))));
                    }

                }
            });
        }

        /**
         * 设置TextView属性
         *
         * @param textView     textView
         * @param sequence     sequence
         * @param textColor    textColor
         * @param textSize     textSize
         * @param gravity      gravity
         * @param fakeBoldText fakeBoldText
         */
        protected void setTextAttribute(Text textView, String sequence,
                                        ColorStateList textColor, int textSize, int gravity, boolean fakeBoldText) {
            if (textView == null) {
                return;
            }
            if (TextTool.isNullOrEmpty(sequence)) {
                return;
            }
            textView.setLineSpacing(mLineSpacingExtra, mLineSpacingMultiplier);
            textView.setTextAlignment(gravity);
            textView.setText(sequence);
            textView.setTextSize(textSize, mTextSizeUnit);

            if (fakeBoldText) {
                textView.setFont(Font.DEFAULT_BOLD);
            }
//            textView.getPaint().setFakeBoldText(fakeBoldText);
            textView.setTextColor(new Color(textColor.mColors[0]));
        }

        /**
         * 设置TextView属性
         *
         * @param textView     textView
         * @param sequence     sequence
         * @param textColor    textColor
         * @param textSize     textSize
         * @param gravity      gravity
         * @param fakeBoldText fakeBoldText
         */
        protected void setTextAttribute(Text textView, String sequence,
                                        Color textColor, int textSize, int gravity, boolean fakeBoldText) {
            if (textView == null) {
                return;
            }
            textView.setLineSpacing(mLineSpacingExtra, mLineSpacingMultiplier);
            textView.setTextAlignment(gravity);
            textView.setText(sequence);
            textView.setTextSize(textSize, mTextSizeUnit);

            if (fakeBoldText) {
                textView.setFont(Font.DEFAULT_BOLD);
            }
//            textView.getPaint().setFakeBoldText(fakeBoldText);
            if (textColor != null) {
                textView.setTextColor(textColor);
            }
        }

        /**
         * 设置TextView 绘制监听
         *
         * @param textView textView
         */
        protected void setTextViewLine(final Text textView) {
            if (textView == null) {
                return;
            }

            new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                @Override
                public void run() {
                    if (mOnTextViewLineListener != null) {
//                        mOnTextViewLineListener.onTextViewLineListener(textView, textView.get());
                        mOnTextViewLineListener.onTextViewLineListener(textView, 0);
                    }
                }
            });

//            textView.post(new Runnable() {
//                @Override
//                public void run() {
//                    if (mOnTextViewLineListener != null) {
//                        mOnTextViewLineListener.onTextViewLineListener(textView, textView.getLineCount());
//                    }
//                }
//            });
        }

        protected Element getBackground() {
            if (mBackground instanceof ShapeElement) {
                if (mBackgroundRadius > 0) {
                    ShapeElement back = new ShapeElement();
                    back.setCornerRadius(mBackgroundRadius);
                    back.setRgbColors(((ShapeElement) mBackground).getRgbColors());
                    mBackground = back;
                }
            }
            return mBackground;
        }

        /**
         * 设置根布局背景Drawable
         *
         * @param drawable drawable
         * @return 设置根布局背景Drawable
         */
        public T setBackground(Element drawable) {
            this.mBackground = drawable;
            return backBuilder();
        }

        protected int dp2px(double dipValue) {

            float dpi = display.getAttributes().densityPixels;
            return (int) (dipValue * dpi + 0.5d * (dipValue >= 0 ? 1 : -1));
        }

        protected int getScreenHeight() {
            return display.getAttributes().height;
        }

        protected int getScreenWidth() {
            return display.getAttributes().width;
        }
    }


}
